<template>
  <!-- 为了有动画效果，用transition标签包裹起来 -->
  <transition name="dialog-fade">
    <!-- 为了点击遮罩层能隐藏隐藏dialog 给遮罩层一个点击事件,为了防止点击表单内部会冒泡到遮罩层盒子，所以加.self修饰符-->
    <div
      class="my-dialog__wrapper"
      v-if="showDialog"
      @click.self="$emit('update:showDialog', false)"
    >
      <div class="my-dialog" style="margin-top:15vh">
        <!-- 标题部分 -->
        <div class="my-dialog__header">
          <!-- 标题使用插槽，让父组键传入 -->
          <slot name="title" class="my-dialog__title">标题</slot>

          <!-- 关闭按钮 -->
          <button
            v-if="showClose"
            type="button"
            aria-label="Close"
            class="my-dialog__headerbtn"
            @click="$emit('update:showDialog', false)"
          >
            <i class="iconfont icon-close"></i>
          </button>
        </div>
        <!-- 主体 -->
        <div class="my-dialog__body">
          <!-- 用默认插槽来接收父组件传过来的内容 -->
          <slot></slot>
        </div>
        <!-- 脚部 -->
        <div class="my-dialog__footer">
          <slot name="footer">底部</slot>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    showClose: {
      type: Boolean,
      default: true
    }
  }
}
</script>

<style lang="less">
// 定义动画
@keyframes dialog-fade {
  from {
    transform: translateY(-10px);
    opacity: 0;
  }
  50% {
    transform: translateY(1px);
    opacity: 0.5;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
// 进入的动画
.dialog-fade-enter-active {
  animation: dialog-fade 0.6s;
}
// 离开的动画
.dialog-fade-leave-active {
  animation: dialog-fade 0.6s reverse;
}
// 后面的省略

.my-dialog__wrapper {
  z-index: 2011;
  margin: 0;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  background-color: rgba(0, 0, 0, 0.6);
  .my-dialog {
    position: relative;
    margin: 0 auto 50px;
    background: #fff;
    border-radius: 2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    width: 50%;
    .my-dialog__header {
      padding: 20px 20px 10px;
      .my-dialog__title {
        line-height: 24px;
        font-size: 18px;
        color: #303133;
      }
      .my-dialog__headerbtn {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 0;
        background: transparent;
        border: none;
        outline: none;
        cursor: pointer;
        font-size: 16px;
        .iconfont {
          color: #909399;
        }
      }
    }
    .my-dialog__body {
      padding: 30px 20px;
      color: #606266;
      font-size: 14px;
      word-break: break-all;
    }
    .my-dialog__footer {
      padding: 10px 20px 20px;
      text-align: right;
      box-sizing: border-box;
    }
  }
}
</style>
